package test.popup.actions;




import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmt.modisco.java.Model;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import artefact.generic.Artefact;
import fr.lip6.meta.ple.plextraction.Extract;
import fr.lip6.meta.tools.ModiscoJavaModelLoader;

public class NewAction implements IObjectActionDelegate {

	@SuppressWarnings("unused")
	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public NewAction() 
	{
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}
	
	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {

		IFolder folder=null;
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		int N=0;
		ArrayList<Artefact> artefacts = new ArrayList<Artefact>();
		Map<String, Model> models = new HashMap<String, Model>();
		String[] products =null;
		if (window != null)
		{
			IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
			Object firstElement = selection.getFirstElement();
			if (firstElement instanceof IAdaptable)
			{    
			folder = (IFolder)((IAdaptable)firstElement).getAdapter(IFolder.class);
			System.out.println("File name is"+folder.getLocationURI().getPath());
			File f = new File(folder.getLocationURI().getPath());
			if(f.isDirectory())
			{
				File []allfiles=f.listFiles();
				products=new String[allfiles.length];
				N=allfiles.length;
				for(int i=0;i<allfiles.length;i++)
				{
					System.out.println(allfiles[i].getAbsolutePath());
					products[i]=allfiles[i].getAbsolutePath();
				}
			}
			System.out.println("Test");

			for (int i=0; i<N; i++) {
				ModiscoJavaModelLoader modiscoLoader = new ModiscoJavaModelLoader();
				String fileName = products[i];
				Model model = ModiscoJavaModelLoader.loadModiscoJavaModel(fileName);
				Artefact artefact;

				modiscoLoader.createArtefact(model, fileName);

				artefact = modiscoLoader.getProduct();
				artefact.setId(i);
				artefacts.add(artefact);

				models.put(fileName, model);
			}
			
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IProject project = root.getProject("FeaturHous");
			
			try {
				project.create(null);
				project.open(null);
				IJavaProject javaProject = JavaCore.create(project); 
				IProjectDescription description = project.getDescription();
				description.setNatureIds(new String[] { JavaCore.NATURE_ID });
				project.setDescription(description, null);

				IClasspathEntry[] buildPath = {
						JavaCore.newSourceEntry(project.getFullPath().append("src")),
				};

				javaProject.setRawClasspath(buildPath, project.getFullPath().append(
						"bin"), null);

				//create folder by using resources package
				IFolder folder1 = project.getFolder("src");

				folder1.create(true, true, null);

				IFolder binFolder = project.getFolder("bin");
				binFolder.create(false, true, null);

				IFolder resultFolder = project.getFolder("generatedfile");
				resultFolder.create(true, true, null);
				IFolder featurefolderr = project.getFolder("Features");
				featurefolderr.create(true, true, null);
				System.out.println(resultFolder.getLocationURI().getPath());
				Extract.extract(artefacts, resultFolder.getLocationURI().getPath(), products,featurefolderr.getLocationURI().getPath());
			}
			catch (CoreException e) 
			{
				e.printStackTrace();
			}
			}
		}
	}
}
